package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.RoomInfoMapper;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private GraphInfoService graphInfoService;

    @Autowired
    private RoomAttrValueService roomAttrValueService;

    @Autowired
    private RoomFacilityService facilityService;

    @Autowired
    private RoomLabelService labelService;

    @Autowired
    private RoomPaymentTypeService paymentTypeService;

    @Autowired
    private RoomLeaseTermService leaseTermService;

    @Autowired
    private RoomInfoMapper infoMapper;


    @Override
    @Transactional
    public void saveOrUpdateRoom(RoomSubmitVo roomSubmitVo) {

        if (roomSubmitVo.getId()!=null){
            Long roomId = roomSubmitVo.getId();
            //1.删除房间图片信息
            LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId,roomId)
                                       .eq(GraphInfo::getItemType,2);
            graphInfoService.remove(graphInfoLambdaQueryWrapper);


            //2.删除房间属性
            LambdaQueryWrapper<RoomAttrValue> roomAttrValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId, roomId);
            roomAttrValueService.remove(roomAttrValueLambdaQueryWrapper);

            //3.房间配套
            LambdaQueryWrapper<RoomFacility> facilityInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            facilityInfoLambdaQueryWrapper.eq(RoomFacility::getRoomId,roomId);
            facilityService.remove(facilityInfoLambdaQueryWrapper);

            //4.房间标签
            LambdaQueryWrapper<RoomLabel> roomLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomLabelLambdaQueryWrapper.eq(RoomLabel::getRoomId, roomId);
            labelService.remove(roomLabelLambdaQueryWrapper);

            //5.支付类型
            LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeQueryWrapper = new LambdaQueryWrapper<>();
            roomPaymentTypeQueryWrapper.eq(RoomPaymentType::getRoomId, roomId);
            paymentTypeService.remove(roomPaymentTypeQueryWrapper);

            //6.租期
            LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermQueryWrapper = new LambdaQueryWrapper<>();
            roomLeaseTermQueryWrapper.eq(RoomLeaseTerm::getRoomId, roomId);
            leaseTermService.remove(roomLeaseTermQueryWrapper);
        }
                //先应该新增房间 才能回显房间号
                this.saveOrUpdate(roomSubmitVo);

                long roomId = roomSubmitVo.getId();

                 //1.新增图片信息
                List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
                List<GraphInfo> graphInfoList = new ArrayList<>(graphVoList.size());
                if (!CollectionUtils.isEmpty(graphVoList)){

                  graphInfoList.forEach(graphVo -> {
                      GraphInfo info = new GraphInfo();
                      info.setName(graphVo.getName());
                      info.setUrl(graphVo.getUrl());
                      info.setItemType(ItemType.ROOM);
                      info.setItemId(roomId);
                      graphInfoList.add(info);
                  });
                  graphInfoService.saveBatch(graphInfoList);
              }

                    //2.新增房间信息
                List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
                List<RoomAttrValue> roomAttrValues = new ArrayList<>(attrValueIds.size());
                if (!CollectionUtils.isEmpty(attrValueIds)){
                    attrValueIds.forEach(attrValueId -> {
                        RoomAttrValue roomAttrValueinfo = RoomAttrValue.builder().attrValueId(attrValueId).roomId(roomId).build();
                        roomAttrValues.add(roomAttrValueinfo);
                    });
                    roomAttrValueService.saveBatch(roomAttrValues);
                }


                //3.房间配套
                List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
                List<RoomFacility> facilityList = new ArrayList<>(facilityInfoIds.size());
                if(!CollectionUtils.isEmpty(facilityInfoIds)) {
                    facilityInfoIds.forEach(facilityInfoId -> {
                        RoomFacility info = new RoomFacility();
                        info.setFacilityId(facilityInfoId).setRoomId(roomId);
                        facilityList.add(info);
                    });
                    //入库操作
                    facilityService.saveBatch(facilityList);
                }

                    //4.新增标签信息
                    List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
                    List<RoomLabel> roomLabelList = new ArrayList<>(labelInfoIds.size());
                    if (!CollectionUtils.isEmpty(labelInfoIds)) {
                        labelInfoIds.forEach(labelInfoId -> {
                            RoomLabel info = new RoomLabel();
                            info.setLabelId(labelInfoId);
                            info.setRoomId(roomId);
                            roomLabelList.add(info);
                        });
                        labelService.saveBatch(roomLabelList);
                    }




                //5.支付类型
                 List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
                List<RoomPaymentType> roomPaymentTypeList = new ArrayList<>(paymentTypeIds.size());
                if (!CollectionUtils.isEmpty(paymentTypeIds)){
                    paymentTypeIds.forEach(paymentTypeId ->{
                        RoomPaymentType info = RoomPaymentType.builder().paymentTypeId(paymentTypeId).roomId(roomId).build();
                        roomPaymentTypeList.add(info);
                    });
                    paymentTypeService.saveBatch(roomPaymentTypeList);
                }

        //6.新增租期
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
                List<RoomLeaseTerm> roomLeaseTermList = new ArrayList<>(leaseTermIds.size());
                if (!CollectionUtils.isEmpty(leaseTermIds)){
                    leaseTermIds.forEach(leaseTermId ->{
                        RoomLeaseTerm info = RoomLeaseTerm.builder().leaseTermId(leaseTermId).roomId(roomId).build();
                        roomLeaseTermList.add(info);
                    });
                    leaseTermService.saveBatch(roomLeaseTermList);
                }


    }

    @Override
    public IPage<RoomItemVo> pageRoomItemByQuery(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
        return infoMapper.pageRoom(page,queryVo);
    }

    @Override
    public RoomDetailVo getRoomDetailById(Long id) {
        return infoMapper.getRoomDetailById(id);
    }

    @Override
    public void removeRoomById(Long id) {
        //1.删除GraphInfo
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, 2)
                                   .eq(GraphInfo::getItemId, id);
        graphInfoService.remove(graphInfoLambdaQueryWrapper);

        //2.删除房间属性
        LambdaQueryWrapper<RoomAttrValue> roomAttrValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId, id);
        roomAttrValueService.remove(roomAttrValueLambdaQueryWrapper);

        //3.房间配套
        LambdaQueryWrapper<RoomFacility> facilityInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        facilityInfoLambdaQueryWrapper.eq(RoomFacility::getRoomId,id);
        facilityService.remove(facilityInfoLambdaQueryWrapper);

        //4.房间标签
        LambdaQueryWrapper<RoomLabel> roomLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomLabelLambdaQueryWrapper.eq(RoomLabel::getRoomId, id);
        labelService.remove(roomLabelLambdaQueryWrapper);

        //5.支付类型
        LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeQueryWrapper = new LambdaQueryWrapper<>();
        roomPaymentTypeQueryWrapper.eq(RoomPaymentType::getRoomId, id);
        paymentTypeService.remove(roomPaymentTypeQueryWrapper);

        //6.租期
        LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermQueryWrapper = new LambdaQueryWrapper<>();
        roomLeaseTermQueryWrapper.eq(RoomLeaseTerm::getRoomId, id);
        leaseTermService.remove(roomLeaseTermQueryWrapper);
    }

}




